var Pexnidi = {};
Pexnidi.kouti = {};

window.onload = function() {
	init();
	Pexnidi.ofelimosXoros();
	Pexnidi.displayStiles();
	Pexnidi.reset();
	Pexnidi.fereData(true);
};

Pexnidi.reset = function() {
	Pexnidi.trepart = null;
	Pexnidi.fasi = null;
Pexnidi.fasi = 'ΣΤΗΣΙΜΟ';
	Pexnidi.epomenos = null;

	for (var i = 1; i <= 6; i++) {
		for (var j = 1; j <= 6; j++) {
			Pexnidi.kouti['k' + i + '' + j] = {
				p:	null,
				c:	null
			};
		}
	}
};

Pexnidi.orataTrapezia = true;

Pexnidi.ofelimosXoros = function() {
	var x = getelid('selida');
	if (notSet(x)) { return; }
	var h = x.clientHeight;
	var w = x.clientWidth;
	x = getelid('main');
	if (notSet(x)) { return; }
	h -= parseInt(x.style.paddingTop);
	h -= parseInt(x.style.paddingBottom);

	x = getelid('fyi');
	if (notSet(x)) { return; }
	h -= x.clientHeight;
	
	var stiliId = [
		'kafenio',
		'controlPanel',
		'tablo',
		'sizitisi'
	];

	var left = 0;
	for (var i = 0; i < stiliId.length; i++) {
		x = getelid(stiliId[i]);
		if (notSet(x)) { continue; }

		x.style.height = h + 'px';
		x.style.left = left + 'px';
		x.style.width = x.clientWidth + 'px';
		left += x.clientWidth;
	}
	x.style.width = (w - left) + 'px';

	x = getelid('sizitisi_kato');
	if (notSet(x)) { return; }
	h -= x.clientHeight;
	x = getelid('sizitisi_pano');
	if (notSet(x)) { return; }
	x.style.height = h + 'px';
};

Pexnidi.displayStiles = function() {
	Pexnidi.displayStiliKafenio();
	Pexnidi.displayStiliControlPanel();
	Pexnidi.displayStiliTablo();
	Pexnidi.displayStiliSizitisi();
};

Pexnidi.displayStiliKafenio = function() {
};

Pexnidi.displayStiliControlPanel = function() {
	var x = getelid('controlPanel');
	if (notSet(x)) { return; }

	x.innerHTML = '';
	x.innerHTML += controlPanel.buttonHTML(Pexnidi.orataTrapezia ?
		'hideTables.png' :'showTables.png', 'Απόκρυψη τραπεζιών',
		'controlPanel.hideShowTables(this)');
	x.innerHTML += controlPanel.buttonHTML('exodos.png', 'Έξοδος',
		'controlPanel.exodos(this)');
};

Pexnidi.displayStiliTablo = function() {
	var x = getelid('tablo');
	if (notSet(x)) { return; }

	var html = '';
	html += '<div id="tp" class="trepart"></div>';

	var w = 1.1;
	var h = 1.6;
	var dw = 0.05;
	var dh = 0.05;
	var pw = w * 0.8;
	var ph = h * 0.8;
	var pt = (h - ph) / 2;
	var pl = (w - pw) / 2;

	html += '<div class="board" style="';
	html += 'width: ' + (8 * (w + dw) - dw) + 'cm;';
	html += 'height: ' + (6 * (h + dh) - dh) + 'cm;';
	html += '">';
	for (var i = 0; i < 6; i++) {
		for (var j = 0; j < 8; j++) {
			var idx = (i + 1) + '' + j;
			html += '<div id="k' + idx + '" class="kouti" style="';
			html += 'top: ' + (i * (h + dh)) + 'cm;';
			html += 'left: ' + (j * (w + dw)) + 'cm;';
			html += 'width: ' + w + 'cm;';
			html += 'height: ' + h + 'cm;';
			html += '" onclick="Pexnidi.klikKouti(' + (i + 1) + ', ' + j + ');">';
			html += '<img class="koutiImage" src="images/kouti.jpg" style="';
			html += 'width: ' + w + 'cm;';
			html += 'height: ' + h + 'cm;';
			html += '" />';
			html += '<img id="p' + idx + '" class="pioni" src="images/pioniN.png" style="';
			html += 'top: ' + pt + 'cm;';
			html += 'left: ' + pl + 'cm;';
			html += 'width: ' + pw + 'cm;';
			html += 'height: ' + ph + 'cm;';
			html += 'display: none;';
			html += '" />';
			html += '</div>';
		}
	}
	html += '</div>';

	x.innerHTML = html;
	Pexnidi.dixeAftia(0);
	Pexnidi.dixeAftia(7);
};

Pexnidi.klikKouti = function(row, col) {
	switch (Pexnidi.fasi) {
	case 'ΣΤΗΣΙΜΟ':
		Pexnidi.topothetisi(row, col);
		break;
	}
};

Pexnidi.dixeAftia = function(j) {
	for (var i = 2; i < 6; i++) {
		var x = getelid('k' + i + '' + j);
		if (notSet(x)) { continue; }
		x.style.visibility = 'hidden';
	}
};

Pexnidi.displayStiliSizitisi = function() {
};

var aaa = 0;
Pexnidi.fereData = function(freska) {
aaa++;
globals.debug.print('fereData: call ' +aaa);
	var req = newRequest();
	if (notSet(req)) { return false; }

	req.onreadystatechange = function() {
		Pexnidi.fereDataCheck(req);
	};
	openRequest(req, 'fantasmatakia/fereData');

	var params = 'login=' + globals.pektis;
	if (notSet(freska)) { freska = false; }
	if (freska) { params += '&freska=yes'; }

	req.send(params);
};

Pexnidi.succerr = 0;

Pexnidi.fereDataCheck = function(req) {
	if (req.readyState != 4) { return; }
globals.debug.print('fereData: get ' +aaa);

	if (Pexnidi.fereDataOk(req)) { 
		Pexnidi.useData(req.responseText);
	}

	if (Pexnidi.succerr > 10) {
		window.location = globals.server + 'error.php?minima=' +
			'successive errors';
		return;
	}

	setTimeout(function() {
		Pexnidi.fereData();
	}, 100);
};

Pexnidi.fereDataOk = function(req) {
	if (req.status != 200) {
		fyi('Δεν παραλήφθησαν δεδομένα! (status = ' + req.status + ')');
		return false;
	}	

//alert(req.responseText);
	if (!req.responseText.match(/@OK$/)) {
		Pexnidi.succerr++;
		fyi('invalid data: ' + req.responseText);
		return false;
	}

	Pexnidi.succerr = 0;
	return true;
};

Pexnidi.useData = function(data) {
globals.debug.on = true;
	globals.debug.print(data);
globals.debug.on = false;
	data = data.replace(/@OK$/, '');
	try {
		eval('var dedomena = {' + data + '}');
	} catch(e) {
		fyi('invalid data: ' + data);
		return;
	}

	if (dedomena.hasOwnProperty('same')) {
		return;
	}

	this.diaxirisiTrepart(dedomena);

	if (dedomena.hasOwnProperty('partida')) {
		this.ftiaxeKafenio(dedomena.partida);	
	}
	else {
		if (dedomena.hasOwnProperty('partidaDel')) {
			this.svisePartida(dedomena.partidaDel);	
		}
		if (dedomena.hasOwnProperty('partidaMod')) {
			this.alaxePartida(dedomena.partidaMod);	
		}
		if (dedomena.hasOwnProperty('partidaAdd')) {
			this.ftiaxeKafenio(dedomena.partidaAdd, true);	
		}
	}

	Pexnidi.processSizitisi(dedomena);
};

Pexnidi.processSizitisi = function(d) {
	if (!d.hasOwnProperty('s')) { return; }
	var x = getelid('sizitisi_pano');
	if (notSet(x)) { return; }

	for (var i = 0; i < d.s.length; i++) {
		var y = document.createElement('div');
		var html = '';
		html += '<div class="sizitisiPektis">' + d.s[i].p + '</div>';
		if (d.s[i].k.match(/^http:\/\/youtu.be\//)) {
			html += '<iframe width="300" height="225" src="' +
				d.s[i].k.replace(/youtu\.be/, 'www.youtube.com/embed') +
				'" frameborder="0" allowfullscreen></iframe>';
		}
		else {
			html += '<div class="sizitisiKimeno">' + d.s[i].k + '</div>';
		}

		y.innerHTML = html;
		x.appendChild(y);
	}

	x.scrollTop = x.scrollHeight;
};

Pexnidi.diaxirisiTrepart = function(d) {
	if (d.hasOwnProperty('tp')) {
		Pexnidi.trepart = d.tp;
		this.enimerosiTablo();
	}
	else {
		Pexnidi.trepart = null;
	}
};

Pexnidi.kathariseTablo = function() {
	var x = getelid('tablo');
	if (notSet(x)) { return; }

	x.innerHTML = '';
};

Pexnidi.enimerosiTablo = function() {
	var x = getelid('tp');
	if (notSet(x)) { return; }

	var html = '';
	html += Pexnidi.trepart; 

	x.innerHTML = html;
};

Pexnidi.ftiaxePartida = function() {
	var req = newRequest();
	if (notSet(req)) { return false; }

	req.onreadystatechange = function() {
		Pexnidi.ftiaxePartidaCheck(req);
	};
	openRequest(req, 'partida/ftiaxePartida');

	req.send();
};

Pexnidi.ftiaxePartidaCheck = function(req) {
	if (req.readyState != 4) {
		return;
	}

	if (req.status != 200) {
		fyi('Απέτυχε η δημιουργία νέας παρτίδας');
		return;
	}	

	fyi(req.responseText);
};

Pexnidi.ftiaxeKafenio = function(p, add) {
	var k = getelid('kafenio');
	if (notSet(k)) { return; }

	var html = '';
	for (var i = 0; i < p.length; i++) {
		html += Partida.HTML(p[i], true);
	}

	if (isSet(add)) {
		html += k.innerHTML;
	}

	k.innerHTML = html;
};

Pexnidi.svisePartida = function(p) {
	for (var i = 0; i < p.length; i++) {
		var k = getelid('p' + p[i]);
		if (notSet(k)) { continue; }
		k.parentNode.removeChild(k);
	}
};

Pexnidi.alaxePartida = function(p) {
	for (var i = 0; i < p.length; i++) {
		var k = getelid('p' + p[i].k);
		if (notSet(k)) { continue; }
		k.innerHTML = Partida.HTML(p[i], false);
	}
};

Pexnidi.topothetisi = function(row, col) {
	var idx = row + '' + col;
	var k = Pexnidi.kouti['k' + idx];
	switch (k.c) {
	case 'B':
		var xroma = 'R';
		break;
	case 'R':
		xroma = 'N';
		break;
	default:
		xroma = 'B';
		break;
	}

	var x = getelid('p' + idx);
	if (notSet(x)) { return; }

	switch (k.c) {
	case 'B':
		var src = 'R';
		var dsp = 'inline';
k.c = 'R';
		break;
	case 'R':
		src = 'N';
		dsp = 'none';
k.c = null;
		break;
	default:
		src = 'B';
		dsp = 'inline';
k.c = 'B';
		break;
	}
	x.src = globals.server + 'images/pioni' + src + '.png';
	x.style.display = dsp;
};
